package com.example.jikeyoujikeyou.office_sz.view.activity.register;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.jikeyoujikeyou.office_sz.R;
import com.example.jikeyoujikeyou.office_sz.model.Department;
import com.example.jikeyoujikeyou.office_sz.model.HttpModel;
import com.example.jikeyoujikeyou.office_sz.service.INetCallBack;
import com.example.jikeyoujikeyou.office_sz.service.StaffService;
import com.example.jikeyoujikeyou.office_sz.view.activity.register.subActivity.Activity_Choice_Department;
import com.example.jikeyoujikeyou.office_sz.view.activity.register.subActivity.EditPersonNameActivity;
import com.example.jikeyoujikeyou.office_sz.view.activity.register.subActivity.EditSeatNameActivity;
import com.example.jikeyoujikeyou.office_sz.view.base.BaseFragment;

public class VAccountFragment extends BaseFragment {
    
    private View activity_register_account;
    private Button next_button;
    private LinearLayout layout_person_name,layout_belong_to_department,layout_seat_name;
    private TextView tv_person_name,tv_belong_to_department,tv_seat_name;
    private boolean flag = false;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 100:
                tv_person_name.setText(data.getStringExtra("person_name"));
                hasNextCondition();
                break;
            case 200:
                tv_belong_to_department.setText(data.getStringExtra("department_name"));
                hasNextCondition();
                break;
            case 300:
                tv_seat_name.setText(data.getStringExtra("seat_name"));
                hasNextCondition();
                break;
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activity_register_account = inflater.inflate(R.layout.activity_register_account,null,false);
        init();
        return activity_register_account;
    }

    private void init() {
        next_button = activity_register_account.findViewById(R.id.next_button);

        layout_person_name = activity_register_account.findViewById(R.id.layout_person_name);
        layout_belong_to_department = activity_register_account.findViewById(R.id.layout_belong_to_department);
        layout_seat_name = activity_register_account.findViewById(R.id.layout_seat_name);

        tv_person_name = activity_register_account.findViewById(R.id.tv_person_name);
        tv_belong_to_department = activity_register_account.findViewById(R.id.tv_belong_to_department);
        tv_seat_name = activity_register_account.findViewById(R.id.tv_seat_name);

        addListener();
    }

    private void addListener(){
        next_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                RegisterActivity.staff.setName(tv_person_name.getText().toString());
                RegisterActivity.staff.setDepartment_name(tv_belong_to_department.getText().toString());
                RegisterActivity.staff.setTitle(tv_seat_name.getText().toString());

                /*System.out.println(RegisterActivity.staff);
                System.out.println(RegisterActivity.company);*/

                StaffService.register(RegisterActivity.staff, RegisterActivity.company, new INetCallBack() {
                    @Override
                    public void onRequstSuccess(HttpModel httpModel) {
                        flag = true;
                    }

                    @Override
                    public void onRequestFail(String msg) {
                        Looper.prepare();
                        new AlertDialog.Builder(getContext()).setTitle("提示").setMessage(msg).setPositiveButton("确定", null).create().show();
                        Looper.loop();
                    }

                    @Override
                    public void onRequestFail() {
                        Looper.prepare();
                        new AlertDialog.Builder(getContext()).setTitle("提示").setMessage("请求失败，请检查网络连接。").setPositiveButton("确定", null).create().show();
                        Looper.loop();
                    }
                });

                if (flag) {
                    message(null);
                }

            }
        });


        layout_person_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().startActivityForResult(new Intent(getActivity(), EditPersonNameActivity.class),1);
            }
        });

        layout_belong_to_department.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().startActivityForResult(new Intent(getActivity(), Activity_Choice_Department.class),1);
            }
        });

        layout_seat_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().startActivityForResult(new Intent(getActivity(), EditSeatNameActivity.class),1);
            }
        });
    }

    private void hasNextCondition(){

        String name = tv_person_name.getText().toString();
        String department = tv_belong_to_department.getText().toString();
        String seat = tv_seat_name.getText().toString();

        if (!isEmpty(name) && !isEmpty(department) && !isEmpty(seat) ) {
            next_button.setEnabled(true);
        } else {
            next_button.setEnabled(false);
        }
    }

    private boolean isEmpty(String str) {
        if (str == null || str.trim().equals("") ) {
            return true;
        }
        return false;
    }

}
